function [capacity_fdma, spectral_efficiency, fig_fdma] = fdma_analysis(EbN0_dB, num_users, bandwidths)
% FDMA多址接入技术分析
% 输入参数：
%   EbN0_dB - 信噪比范围 (dB)
%   num_users - 用户数量数组
%   bandwidths - 带宽分配数组
% 输出参数：
%   capacity_fdma - FDMA容量
%   spectral_efficiency - 频谱效率
%   fig_fdma - 图形句柄

% 添加路径
addpath('../Common');
colors = color_definitions();

% 参数设置
num_subcarriers = 64; % 子载波数量
channel_gains = 1; % 信道增益 (归一化)
noise_power_dBm = -174 + 10*log10(180e3); % 噪声功率 (dBm)
noise_power = 10^((noise_power_dBm-30)/10); % 转换为线性值

% 初始化结果数组
num_snr = length(EbN0_dB);
num_user_cases = length(num_users);
capacity_fdma = zeros(num_snr, num_user_cases);
spectral_efficiency = zeros(num_snr, num_user_cases);

fprintf('FDMA多址接入技术分析...\n');

% FDMA容量计算
for snr_idx = 1:num_snr
    snr_linear = 10^(EbN0_dB(snr_idx)/10);
    
    for user_idx = 1:num_user_cases
        K = num_users(user_idx); % 用户数量
        
        % 每个用户分配的子载波数量
        subcarriers_per_user = floor(num_subcarriers / K);
        bandwidth_per_user = bandwidths(user_idx) / K; % 每用户带宽
        
        % 每用户接收功率 (假设总功率相等分配)
        power_per_user = 1 / K;
        
        % 计算每用户的容量
        user_capacity = 0;
        for sc = 1:subcarriers_per_user
            % 每子载波SNR
            snr_per_subcarrier = power_per_user * channel_gains / noise_power;
            
            % Shannon容量公式
            if snr_per_subcarrier > 0
                user_capacity = user_capacity + bandwidth_per_user * log2(1 + snr_per_subcarrier);
            end
        end
        
        % 总容量 (所有用户)
        capacity_fdma(snr_idx, user_idx) = K * user_capacity;
        
        % 频谱效率
        spectral_efficiency(snr_idx, user_idx) = capacity_fdma(snr_idx, user_idx) / bandwidths(user_idx);
    end
end

%% 可视化结果
fig_fdma = figure('Name', 'FDMA多址接入技术分析', 'Position', [100, 100, 1200, 800]);

% 子图1: FDMA容量 vs SNR
subplot(2, 2, 1);
for user_idx = 1:num_user_cases
    plot(EbN0_dB, capacity_fdma(:, user_idx)/1e6, 'LineWidth', 2, 'Color', colors(user_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('容量 (Mbps)');
title('FDMA容量 vs 信噪比');
legend(arrayfun(@(x) sprintf('%d用户', x), num_users, 'UniformOutput', false), 'Location', 'NorthWest');

% 子图2: 频谱效率 vs SNR
subplot(2, 2, 2);
for user_idx = 1:num_user_cases
    plot(EbN0_dB, spectral_efficiency(:, user_idx), 'LineWidth', 2, 'Color', colors(user_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('频谱效率 (bps/Hz)');
title('FDMA频谱效率 vs 信噪比');
legend(arrayfun(@(x) sprintf('%d用户', x), num_users, 'UniformOutput', false), 'Location', 'NorthWest');

% 子图3: 容量 vs 用户数量
subplot(2, 2, 3);
snr_index = round(num_snr/2); % 中等SNR
plot(num_users, capacity_fdma(snr_index, :)/1e6, 'o-', 'LineWidth', 2, 'Color', colors(1, :));
grid on;
xlabel('用户数量');
ylabel('容量 (Mbps)');
title(sprintf('FDMA容量 vs 用户数量 (Eb/N0 = %d dB)', EbN0_dB(snr_index)));

% 子图4: 用户间干扰分析
subplot(2, 2, 4);
% 计算用户间干扰 (假设相邻信道泄漏)
interference_power = zeros(num_user_cases, 1);
for user_idx = 1:num_user_cases
    K = num_users(user_idx);
    % 假设相邻信道泄漏比为-20dB
    adjacent_channel_leakage = 0.01;
    interference_power(user_idx) = (K-1) * adjacent_channel_leakage;
end
bar(num_users, 10*log10(interference_power + 1e-10));
xlabel('用户数量');
ylabel('干扰功率 (dB)');
title('FDMA用户间干扰分析');

fprintf('FDMA分析完成！\n');

end